package ar.edu.unq.wannaplay.persistence.daos;

import java.util.ArrayList;
import java.util.List;

import ar.edu.unq.wannaplay.calendareable.Project;
import ar.edu.unq.wannaplay.employee.Employee;
import ar.edu.unq.wannaplay.persistence.genericdaos.HibernateGenericDao;

public class ProjectDAO extends HibernateGenericDao<Project> {
    private static final long serialVersionUID = 1L;

    private EmployeeDAO employeeDAO;

    @Override
    protected Class<Project> getDomainClass() {
        return Project.class;
    }

    public void saveProject(final Project project) {
        this.addRealEmployees(project);
        super.save(project);
    }

    public void updateProject(final Project project) {
        this.addRealEmployees(project);
        super.update(project);
    }

    private void addRealEmployees(final Project project) {
        List<Employee> realEmployees = new ArrayList<Employee>();
        for (Employee emp : project.getEmployees()) {
            Employee realEmployee = employeeDAO.findEmployeeByDNI(emp.getData().getId());
            realEmployees.add(realEmployee);
        }

        project.setEmployees(new ArrayList<Employee>());
        for (Employee realEmp : realEmployees) {
            project.addEmployee(realEmp);
            employeeDAO.update(realEmp);
        }
    }

    public EmployeeDAO getEmployeeDAO() {
        return employeeDAO;
    }

    public void setEmployeeDAO(final EmployeeDAO employeeDAO) {
        this.employeeDAO = employeeDAO;
    }
}
